package xju_5_6.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import xju_5_6.entity.User;
import xju_5_6.service.UserService;

import javax.servlet.http.HttpSession;

@Controller
public class MyController {
    @Autowired
    private UserService userService;

    @RequestMapping("/exception")
    public String test11() {
        int a = 1/0;
        return "hello";
    }

//    @ExceptionHandler(Exception.class)
//    public ModelAndView test12(Exception ex) {
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setViewName("error");
//        modelAndView.addObject("exception", "出现了异常" + ex.getMessage());
//        return modelAndView;
//    }
    @RequestMapping("/login")
    public String test22(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
        User user = userService.login(username, password);
        if (user != null) {
            session.setAttribute("user", user);
            return "hello";
        }
        return "login";
    }
}
